package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Html.ImageGetter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.TipsBean;
import com.zhongxi.guoyuan.entity.TipsDetailBean;
import com.zhongxi.guoyuan.utils.RegularTool;
import com.zhongxi.guoyuan.widget.webview.RichTextView;

import java.util.List;

/**
 * Created by Administrator on 2017/2/4.
 */

public class TipsAdapter extends BaseAdapter {

    private Context mContext;
    private List<TipsBean.TipsData> data;
    private int mExpandedMenuPos = -1;
    private TipsDetailBean.TipsDetail details;
    private ImageGetter ig = null;

    public TipsAdapter(Context context, List<TipsBean.TipsData> data) {
        this.mContext = context;
        this.data = data;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos, TipsDetailBean.TipsDetail data) {
        this.mExpandedMenuPos = mExpandedMenuPos;
        this.details = data;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos){
        this.mExpandedMenuPos = mExpandedMenuPos;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_tips, parent, false);
            holder.tvText = (TextView) convertView.findViewById(R.id.tvText);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.rela= (RelativeLayout) convertView.findViewById(R.id.rela);
            holder.tvContent = (RichTextView) convertView.findViewById(R.id.tvContent);
            holder.relaInfo = (RelativeLayout) convertView.findViewById(R.id.relaInfo);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.relaInfo.setVisibility(position == mExpandedMenuPos?View.VISIBLE:View.GONE);
        if (position==mExpandedMenuPos){
            holder.rela.setBackground(mContext.getResources().getDrawable(R.drawable.bg_red_skolid));
            holder.tvText.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.ivImg.setVisibility(View.GONE);
        }else {
            holder.rela.setBackground(mContext.getResources().getDrawable(R.drawable.bg_white_solid));
            holder.tvText.setTextColor(mContext.getResources().getColor(R.color.bg_yield));
            holder.ivImg.setVisibility(View.VISIBLE);
        }
        holder.ivImg.setImageResource(position == mExpandedMenuPos ? R.mipmap.iv_down_light:R.mipmap.iv_down);
        if(null!=data.get(position).getTitle()&&!"".equals(data.get(position).getTitle())){
            holder.tvText.setText(data.get(position).getTitle());
        }else{
            holder.tvText.setText("");
        }
        final ViewHolder hol = holder;

        if(null!=details&&!"".equals(details)) {
            if (null != details.getDescription() && !"".equals(details.getDescription())) {
                RegularTool.initImageLoader(mContext);
                holder.tvContent.setRichText(details.getDescription());
            };
        };
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mExpandedMenuPos = -1;
//                notifyDataSetChanged();
//            }
//        });
        return convertView;
    }

    /*public void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
                context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());

    }*/

    /*private String getNewContent(String htmltext){

        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }
        return doc.toString();
    }
*/
    class ViewHolder{
        private TextView tvText;
        private ImageView ivImg;
        private RelativeLayout rela;
        private RichTextView tvContent;
        private RelativeLayout relaInfo;
    }
}
